from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

from django.utils.translation import gettext_lazy as _

# 候选人学历
DEGREE_TYPE = ((u'本科', u'本科'), (u'硕士', u'硕士'), (u'博士', u'博士'))

JobTypes = [
    (0, "技术类"),
    (1, "产品类"),
    (2, "运营类"),
    (3, "设计类"),
    (4, "市场营销类")
]

Cities = [
    (0, "北京"),
    (1, "上海"),
    (2, "深圳"),
    (3, "杭州"),
    (4, "广州")
]


class Job(models.Model):
    # Translators: 职位实体的翻译
    job_type = models.SmallIntegerField(blank=False, choices=JobTypes, verbose_name=_("职位类别"))
    job_name = models.CharField(max_length=250, blank=False, verbose_name=_("职位名称"))
    job_city = models.SmallIntegerField(choices=Cities, blank=False, verbose_name=_("工作地点"))
    job_responsibility = models.TextField(max_length=1024, verbose_name=_("职位职责"))
    job_requirement = models.TextField(max_length=1024, blank=False, verbose_name=_("职位要求"))
    creator = models.ForeignKey(User, verbose_name=_("创建人"), null=True, on_delete=models.SET_NULL)
    created_date = models.DateTimeField(verbose_name=_("创建日期"), auto_now_add=True)
    modified_date = models.DateTimeField(verbose_name=_("修改日期"), auto_now=True)

    class Meta:
        verbose_name = _('职位')
        verbose_name_plural = _('职位列表')

    def __str__(self):
        return self.job_name


class Resume(models.Model):
    # Translators: 简历实体的翻译
    username = models.CharField(max_length=135, verbose_name=_('姓名'))
    applicant = models.ForeignKey(User, verbose_name=_("申请人"), null=True, on_delete=models.SET_NULL)
    city = models.CharField(max_length=135, verbose_name=_('当前所在城市'), null=True)
    phone = models.CharField(max_length=135, verbose_name=_('手机号码'), null=True)
    email = models.EmailField(max_length=135, blank=True, verbose_name=_('邮箱'), null=True)
    apply_position = models.CharField(max_length=135, blank=True, verbose_name=_('应聘职位或期望职位'), null=True)
    age = models.CharField(max_length=2, verbose_name='年龄', blank=True, null=True)
    born_address = models.CharField(max_length=135, blank=True, verbose_name=_('生源地'), null=True)
    gender = models.CharField(max_length=135, blank=True, verbose_name=_('性别'), null=True)
    picture = models.ImageField(upload_to='images/', blank=True, verbose_name=_('个人照片'), null=True)
    attachment = models.FileField(upload_to='file/', blank=True, verbose_name=_('简历附件'), null=True)

    # 学校与学历信息
    bachelor_school = models.CharField(max_length=135, blank=True, verbose_name=_('本科学校'), null=True)
    master_school = models.CharField(max_length=135, blank=True, verbose_name=_('研究生学校'), null=True)
    doctor_school = models.CharField(max_length=135, blank=True, verbose_name=u'博士生学校', null=True)
    major = models.CharField(max_length=135, blank=True, verbose_name=_('专业'), null=True)
    degree = models.CharField(max_length=135, choices=DEGREE_TYPE, blank=True, verbose_name=_('学历'), null=True)
    created_date = models.DateTimeField(verbose_name="创建日期", default=datetime.now, null=True)
    modified_date = models.DateTimeField(verbose_name="修改日期", auto_now=True, null=True)

    # 候选人自我介绍，工作经历，项目经历
    candidate_introduction = models.TextField(max_length=1024, blank=True, verbose_name=u'自我介绍', null=True)
    work_experience = models.TextField(max_length=1024, blank=True, verbose_name=u'工作经历', null=True)
    project_experience = models.TextField(max_length=1024, blank=True, verbose_name=u'项目经历', null=True)

    # path
    raw_path = models.CharField(max_length=255, blank=True, null=True, verbose_name=_('管理员上传原简历后保存路径'), unique=True)
    parse_path = models.CharField(max_length=255, blank=True, null=True, verbose_name=_('管理员上传原简历后保存路径'), unique=True)
    raw_content = models.BinaryField(verbose_name=_('原简历二进制内容'), null=True, blank=True)
    parse_content = models.BinaryField(verbose_name=_('原简历二进制内容'), null=True, blank=True)
    raw_prefix = models.CharField(max_length=11, verbose_name='原简历文件后缀', null=True, blank=True)
    parse_prefix = models.CharField(max_length=11, verbose_name='简历文件后缀', null=True, blank=True, default='.docx')
    uploader = models.ForeignKey(User, verbose_name='上传者', null=True, blank=True, on_delete=models.SET_NULL, related_name='my_upload_resumes')

    class Meta:
        verbose_name = _('简历')
        verbose_name_plural = _('简历列表')

    def __str__(self):
        return self.username

    def basic_info_to_dict(self):
        """将简历实体的翻译转换成字典"""
        return {
            'head': '个人信息',
            'data': {
                '姓名': self.username,
                '性别': self.gender,
                '年龄': self.age,
                '手机号码': self.phone,
                '邮箱': self.email,
                '专业': self.major,
                '毕业学校': self.bachelor_school,
            }
        }

    def school_educate_to_dict(self):
        """将学校与学历信息转换成字典"""
        return {
            'head': '学校与学历',
            'data': {
                '本科学校': self.bachelor_school,
                '研究生学校': self.master_school,
                '博士生学校': self.doctor_school,
                '专业': self.major,
                '学历': self.degree,
            }
        }

    def detail_to_dict(self):
        """将候选人自我介绍，工作经历，项目经历转换成字典"""
        return {
            'head': '自我介绍,工作经历,项目经历',
            'data': {
                '自我介绍': self.candidate_introduction,
                '工作经历': self.work_experience,
                '项目经历': self.project_experience,
            }
        }

    def merge_dict(self):
        return {
            'data_list': [self.basic_info_to_dict(), self.school_educate_to_dict(), self.detail_to_dict()]
        }

    def get_companies(self):
        company_lst = [company.to_list() for company in self.companies.all()]
        company_lst.insert(0, ['公司名称', '开始时间', '结束时间'])
        print(company_lst)
        return company_lst

    def get_expirience(self):
        pro = [project.to_dic() for project in self.projects.all()]
        print(pro)
        return pro


class CompanyWorkYear(models.Model):
    resume = models.ForeignKey(Resume, verbose_name='简历', on_delete=models.CASCADE, null=True, blank=True, related_name='companies')
    name = models.CharField(max_length=60, verbose_name='公司名称', null=True, blank=True)
    start_time = models.CharField(max_length=60, verbose_name='开始时间', null=True, blank=True)
    end_time = models.CharField(max_length=60, verbose_name='结束时间', null=True, blank=True)

    def to_list(self):
        return [self.name, self.start_time, self.end_time]


class ProjectExperience(models.Model):
    resume = models.ForeignKey(Resume, verbose_name='简历', on_delete=models.CASCADE, null=True, blank=True,
                               related_name='projects')
    project_name = models.CharField(max_length=255, verbose_name='项目名称')
    project_brief = models.TextField(max_length=1024, verbose_name='项目简介')

    def to_dic(self):

        return {
            '项目名': self.project_name,
            '项目简介': self.project_brief
        }






















